﻿using rmTest.Common.AttrUtil;
using rmTest.Common.ModelUtil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace rmTest.Common.ExtensionUtil
{
    /// <summary>
    /// 扩张方法类型
    /// </summary>
    public static class ExtensionMethod
    {
        public static ValidateResult Validate<T>(this T t, bool isVaidateAll = false)
        {
            ValidateResult result = new ValidateResult();
            result.ValidateMsg = new Dictionary<string, string>();
            result.isValidateAll = isVaidateAll;
            result.isValidated = true;
            Type type = t.GetType();
            foreach (var prop in type.GetProperties())
            {
                if (prop.IsDefined(typeof(ModelBaseValidateAttribute), true))
                {
                    object[] attributeArray = prop.GetCustomAttributes(typeof(ModelBaseValidateAttribute), true);
                    foreach (ModelBaseValidateAttribute attribute in attributeArray)
                    {
                        var v = prop.GetValue(t);
                        result.ValidateMsg.Add(prop.Name, string.Empty);
                        attribute.Validate(attribute, v, result);
                        if (!isVaidateAll)
                        {
                            return result;
                        }
                    }
                }
            }
            return result;
        }


        public static string GetRemark(this Enum value)
        {
            Type type = value.GetType();
            FieldInfo field = type.GetField(value.ToString());
            if (field.IsDefined(typeof(DisplayBaseAttribute), true))
            {
                DisplayBaseAttribute attribute = (DisplayBaseAttribute)field.GetCustomAttribute(typeof(DisplayBaseAttribute), true);
                return attribute.GetDisplayName();
            }
            else
            {
                return value.ToString();
            }
        }

        /// <summary>
        /// 获取显示属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        [Obsolete("此方法调用者没有限制，不允许使用，请使用 GetModelDisplayValue 或者 GetMemberInfoDisplayValue", true)]
        public static string GetDisplayValue<T>(this T t)
        {
            Type type = typeof(T);

            string name = string.Empty;// ((PropertyInfo)t).Name;
            PropertyInfo property = null;
            //判断传进来的属性还是类
            if (type.Name == nameof(PropertyInfo) || type.Name == nameof(FieldInfo))
            {
                property = t as PropertyInfo;
                name = property.Name;
            }
            else
            {
                name = type.Name;
            }
            bool isHaveAttr = false;
            if (type.Name == nameof(PropertyInfo))
                isHaveAttr = property.IsDefined(typeof(DisplayBaseAttribute), true);
            else
                isHaveAttr = type.IsDefined(typeof(DisplayBaseAttribute), true);
            if (isHaveAttr)
            {
                object[] attrs;
                if (type.Name == nameof(PropertyInfo))
                    attrs = property.GetCustomAttributes(typeof(DisplayBaseAttribute), false);
                else
                    attrs = type.GetCustomAttributes(typeof(DisplayBaseAttribute), false);
                DisplayBaseAttribute attr = (DisplayBaseAttribute)attrs.FirstOrDefault();
                if (attr != null)
                {
                    name = attr.Name;
                }
            }
            return name;
        }

        /// <summary>
        /// 获取显示属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetModelDisplayValue<T>(this T t) where T : BaseModel
        {
            Type type = typeof(T);

            string name = string.Empty;// ((PropertyInfo)t).Name;
            PropertyInfo property = null;
            //判断传进来的属性还是类
            name = type.Name;
            bool isHaveAttr = false;
            if (type.Name == nameof(PropertyInfo))
                isHaveAttr = property.IsDefined(typeof(DisplayBaseAttribute), true);
            else
                isHaveAttr = type.IsDefined(typeof(DisplayBaseAttribute), true);
            if (isHaveAttr)
            {
                object[] attrs = type.GetCustomAttributes(typeof(DisplayBaseAttribute), false);
                DisplayBaseAttribute attr = (DisplayBaseAttribute)attrs.FirstOrDefault();
                if (attr != null)
                {
                    name = attr.Name;
                }
            }
            return name;
        }

        /// <summary>
        /// 获取显示属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetMemberInfoDisplayValue<T>(this T t) where T : MemberInfo
        {
            Type type = typeof(T);

            string name = string.Empty;// ((PropertyInfo)t).Name;
            PropertyInfo property = null;
            //判断传进来的属性还是类
            if (type.Name == nameof(PropertyInfo) || type.Name == nameof(FieldInfo))
            {
                property = t as PropertyInfo;
                name = property.Name;
            }
            bool isHaveAttr = false;
            if (type.Name == nameof(PropertyInfo))
                isHaveAttr = property.IsDefined(typeof(DisplayBaseAttribute), true);
            else
                isHaveAttr = type.IsDefined(typeof(DisplayBaseAttribute), true);
            if (isHaveAttr)
            {
                object[] attrs;
                if (type.Name == nameof(PropertyInfo))
                    attrs = property.GetCustomAttributes(typeof(DisplayBaseAttribute), false);
                else
                    attrs = type.GetCustomAttributes(typeof(DisplayBaseAttribute), false);
                DisplayBaseAttribute attr = (DisplayBaseAttribute)attrs.FirstOrDefault();
                if (attr != null)
                {
                    name = attr.Name;
                }
            }
            return name;
        }
    }
}
